package ioc15;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

/**
 * Author:shixiaojun@itany.com
 * Date:2022/3/7 14:52
 * Version:1.0-SNAPSHOT
 */
public class UpperCaseBeanPostProcessor implements BeanPostProcessor {

    /**
     * 在初始化之前做后处理
     * @param bean          当前进入到后处理bean中的bean是谁
     * @param beanName      当前进入到后处理bean中的bean的id属性值是多少
     * @return              处理之后返回的bean
     * @throws BeansException
     */
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        // 判断当前进入的bean是否是SomeBean
        if(bean instanceof SomeBean){
            // 所有的SomeBean首字母大写
            SomeBean someBean = (SomeBean) bean;
            someBean.setName(someBean.getName().substring(0,1).toUpperCase()+someBean.getName().substring(1));
        }
        // 判断当前进入的bean的id属性值是否是otherBean
        if("otherBean".equals(beanName)){
            // id为otherBean的bean的name属性值全部大写
            OtherBean otherBean = (OtherBean) bean;
            otherBean.setName(otherBean.getName().toUpperCase());
        }
        // otherBean2改为首字母大写,其他字母小写
        if("otherBean2".equals(beanName)){
            OtherBean otherBean = (OtherBean) bean;
            otherBean.setName(otherBean.getName().substring(0,1).toUpperCase()+otherBean.getName().substring(1).toLowerCase());
        }
        return bean;
    }
}
